package com.lvm.app.task;

import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class VideoTaskHolder {

    private VideoTaskExecutor executor = new VideoTaskExecutor();
    private LinkedList<VideoTask> task = new LinkedList<>();


    public List<VideoTask> queryTask(String taskId){

        if(taskId!=null){
            return task.stream().filter((item)->{
                return item.getTaskId().equals(taskId);
            }).collect(Collectors.toList());
        }else{
            return task;
        }

    }

    public boolean addTask(VideoTask videoTask){
        task.add(videoTask);
        try {
            executor.addTask(videoTask);
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    public boolean removeTask(String taskId){
        Iterator<VideoTask> ite = task.iterator();
        while (ite.hasNext()){
            VideoTask t = ite.next();
           if(t.getTaskId().equals(taskId)){
                t.setStatus(-1);
           }
        }


        return true;
    }


}
